import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-11-11
 * Time: 16:24
 */
public class test   {
    public static int[] findNext(String P) {
        int i = 0;
        int k = -1;
        int m = P.length() + 1;
        int[] next = new int[m];
        next[0] = -1;
        while (i < m - 1){
            if (k == -1 || P.charAt(k) == P.charAt(i)) next[++i] = ++k;
            else k = next[k];
        }
        return next;
    }

    public static void main(String[] args) {
        String P = "aaaa";
        int[] arr = findNext(P);
        System.out.println(Arrays.toString(arr));
    }
}
